﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using Oracle.DataAccess.Client;
using System.Data;
using LCUV;

namespace sWarranty.WEB
{
    public partial class FindBill : System.Web.UI.Page
    {
        public string _currentUserEmpNo = "";
        public string _currentUserAuthority = "";
        public string _currentUserRegion = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            _currentUserEmpNo = GetUserLoginEmpNo();
            btnFind.Focus();

            string shcode = "";
            try
            {
                //Save case
                shcode = Request.QueryString["d"];
            }
            catch (Exception)
            {
                //Add case
            }

            if (!string.IsNullOrEmpty(shcode) && !IsPostBack)
            {
                litExistsBillTo.Text = "";
                txtReturnValueBillTo.Text = DBAction.GetBillToByShopCode(shcode);
                //set default len dau
                Entities db = new Entities();
                var d = from t in db.TB_CM_SHOP_INF
                        where t.SHOP_CODE == shcode
                        select t;
                if (d.Count() == 1)
                {
                    txtReturnValueBillTo.Text = txtReturnValueBillTo.Text.Replace(d.Single().BILLTO_CODE + ",", "");
                    txtReturnValueBillTo.Text = d.Single().BILLTO_CODE + "," + txtReturnValueBillTo.Text;
                }

                ////build exists billto table (tblright)
                //string[] mang = txtReturnValueBillTo.Text.Split(',');
                //int i = 1;
                //foreach (string item in mang)
                //{
                //    if (!string.IsNullOrEmpty(item))
                //        if (i == 1)//set default cho item dau tien
                //            litExistsBillTo.Text += "<tr id='row_" + item + "'><td><input type='checkbox' onclick='toogleReturnValueToLeft('" + item + "')' id='chkRight_" + item + "'></td><td><input type='radio' onclick='setDefaul('" + item + "')' id='rad_" + item + "' name='group' checked='checked'></td><td>" + item + "</td></tr>";
                //        else
                //            litExistsBillTo.Text += "<tr id='row_" + item + "'><td><input type='checkbox' onclick='toogleReturnValueToLeft('" + item + "')' id='chkRight_" + item + "'></td><td><input type='radio' onclick='setDefaul('" + item + "')' id='rad_" + item + "' name='group'></td><td>" + item + "</td></tr>";
                //    i = 2;
                //}
            }
        }
        protected string GetUserLoginEmpNo()
        {
            string t = "";
            try
            {
                if (Session["UserLoginInfo"] != null)
                {
                    Entities db = new Entities();
                    var u = (TB_USER[])Session["UserLoginInfo"];
                    int _UID = Convert.ToInt32(u.Single().ID);
                    var au = from ft in db.TB_USER_LEVEL
                             where ft.USER_ID == _UID
                             select ft;
                    int _le = Convert.ToInt32(au.Single().LEVEL_ID);
                    var le = from xx in db.TB_LEVEL
                             where xx.ID == _le
                             select xx;
                    _currentUserAuthority = le.Single().REMARK;
                    _currentUserRegion = u.Single().REGION_ID;
                    //if (_currentUserRegion != "00")
                    //{
                    //    DBAction.SetSelected(dropRegion, DBAction.GetRegionByID(u.Single().REGION_ID));
                    //    dropRegion.Enabled = false;
                    //}
                    return u.Single().EMPNO;
                }
            }
            catch (Exception)
            {
                t = "";
            }
            return t;
        }

        protected void btnFind_Click(object sender, EventArgs e)
        {
            litData.Text = "";
            Entities db = new Entities();
            var bill = from t in db.TB_CM_BILLTO_INF
                       select t;
            if (!string.IsNullOrEmpty(txtBill.Text))
                bill = bill.Where(k => k.BILLTO_CODE.ToLower().Contains(txtBill.Text.ToLower()));
            if (!string.IsNullOrEmpty(txtFullName.Text))
            {
                if (bill.Where(k => k.FULL_NAME.ToLower().Contains(txtFullName.Text.ToLower())).Count() <= 0) 
                    //ko co' name nay, thi tim theo address
                    bill = bill.Where(k => k.ADDRESS.ToLower().Contains(txtFullName.Text.ToLower()));
                else
                    bill = bill.Where(k => k.FULL_NAME.ToLower().Contains(txtFullName.Text.ToLower()));
            }
            //if (!string.IsNullOrEmpty(txtRepresentative.Text))
            //    bill = bill.Where(k => k.REPRESENTATIVE.ToLower().Contains(txtRepresentative.Text.ToLower()));
            //if (dropRegion.SelectedValue != "0")
            //    bill = bill.Where(k => k.LOW_PRICE_GROUP == dropRegion.SelectedValue);

            if (bill.Count() > 0)
            {
                foreach (var item in bill)
                {
                    //string returnValue = item.BILLTO_CODE + "&" + item.FULL_NAME + "|" + item.CUSTOMER_CODE + "&" + item.ADDRESS;//item.BILLTO_CODE + " - " + item.FULL_NAME + "|" + item.CUSTOMER_CODE + " - " + item.ADDRESS;
                    litData.Text += "<tr id='tr" + item.BILLTO_CODE + "'>";
                    litData.Text += "   <td> <input id='" + item.BILLTO_CODE + "' type='checkbox' onclick=\"toogleReturnValue('" + item.BILLTO_CODE + "')\" />";
                    litData.Text += "   </td>";
                    litData.Text += "   <td>" + item.BILLTO_CODE;
                    litData.Text += "   </td>";
                    litData.Text += "   <td>" + item.FULL_NAME;
                    litData.Text += "   </td>";
                    litData.Text += "</tr>";
                }
            }
            else
            {

                MessageBox.Show("No BillTO Code found");
            }
        }



    }
}